mod database;
mod employee;
use database::Database;
use std::io;

fn display_menu() -> i32 {
    println!("Employee Database");
    println!("-- -- -");
    println!("1)Hire a new employee");
    println!("2)Fire an employee");
    println!("3)Promote an employee");
    println!("4)List all employees");
    println!("5)List all current employees");
    println!("6)List all former employees");
    println!("0)Quit");

    let mut input = String::new();
    io::stdin().read_line(&mut input).expect("unexcept input");

    let number: i32 = input.trim().parse().expect("inalid number");

    number
}

fn do_hire(db: &mut Database) {
    let mut first_name = String::new();
    println!("First name?");
    io::stdin()
        .read_line(&mut first_name)
        .expect("unexcept input");

    let mut last_name = String::new();
    println!("Last name?");
    io::stdin()
        .read_line(&mut last_name)
        .expect("unexcept input");

    let new_employee = db.add_employee(first_name, last_name);

    println!(
        "Hired employee {}·{} with number {}",
        new_employee.get_first_name(),
        new_employee.get_last_name(),
        new_employee.get_employee_number()
    );
}

fn do_fire(db: &mut Database) {
    println!("Employee number?");
    let mut input = String::new();
    io::stdin().read_line(&mut input).expect("unexcept input");

    let employee_number: i32 = input.trim().parse().expect("inalid number");

    let emp = db.get_employee_by_number(employee_number);

    emp.fire();

    println!("Employee {} terminated.", emp.get_employee_number());
}

fn do_promote(db: &mut Database) {
    println!("Employee number?");
    let mut input = String::new();
    io::stdin().read_line(&mut input).expect("unexcept input");

    let employee_number: i32 = input.trim().parse().expect("inalid number");

    io::stdin().read_line(&mut input).expect("unexcept input");

    let raise_amount: i32 = input.trim().parse().expect("inalid number");

    let emp = db.get_employee_by_number(employee_number);

    emp.promote(raise_amount);
}

fn main() {
    let mut employee_db = Database::new();
    let mut done = false;

    while !done {
        match display_menu() {
            0 => done = true,
            1 => do_hire(&mut employee_db),
            2 => do_fire(&mut employee_db),
            3 => do_promote(&mut employee_db),
            4 => employee_db.display_all(),
            5 => employee_db.display_current(),
            6 => employee_db.display_former(),
            _ => println!("Unknown Commnad"),
        }
    }
}
